/*Author Rajith Chathunga,
 *		 Computer Science and Engineering,
 *		 University of Moratuwa.
 *
 * Class name : QNews
 * 
 *  			
 * This class shows the news presenting style of this project. This class extends Activity and
 *  implements onClickListener.*/

package chathunga.qNews;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class QNews extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	TextView newsbar;
	ImageButton foword;
	ImageButton rew;
	TextView views;
	ImageView img;
	ImageView img2;
	String newsURL;
	String inputUrl;
	ArrayList<RSS> newItems;
	Intent inte = new Intent();
	int i;
	int j;
	int k;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bundle b = getIntent().getExtras();
        inputUrl = b.getString("key");
        newsURL=inputUrl;
        inte.setAction(Intent.ACTION_VIEW);
        newItems = RSS.getRssItems(newsURL);
        j=newItems.size();
        newsbar=(TextView)findViewById(R.id.nbar);
        foword=(ImageButton)findViewById(R.id.fw);
        rew=(ImageButton)findViewById(R.id.rw);
        img=(ImageView)findViewById(R.id.widget41);
        img2=(ImageView)findViewById(R.id.click);
        views=(TextView)findViewById(R.id.views);
        img.setOnClickListener(this);
        newsbar.setOnClickListener(this);
        foword.setOnClickListener(this);
        rew.setOnClickListener(this);
        img2.setOnClickListener(this);
        newsbar.setText(newItems.get(0).toString());
        views.setVisibility(View.GONE);
        views.setText("");
        img2.setVisibility(View.GONE);
        k=0;
        i=1;
        
    }

	@Override
	public void onClick(View v) {
		
			if(v.getId()==R.id.fw){
				
				views.setVisibility(View.GONE);
				
				img2.setVisibility(View.GONE);
				
				for(k=k;k<=j;){
					
					k++;
					
					if(k==j){
						
						k=0;
					}
					newsbar.setText(newItems.get(k).toString());
					
					break;
				}
				
			}
			
					
	//when reverse button clicked	

		if(v.getId()==R.id.rw){
			
			views.setVisibility(View.GONE);
			
			img2.setVisibility(View.GONE);
			
			for(k=k;k<=j;){
				
				if(k==0){
					
					k=j;
				}
				
				newsbar.setText(newItems.get(--k).toString());
				
				break;
			}
			
			
		}
	//when clicked on news bar
		if(v.getId()==R.id.nbar){
			
			views.setVisibility(View.VISIBLE);
			
			img2.setVisibility(View.VISIBLE);
			
			for(k=k;k<=j;){
				
				views.setText(newItems.get(k).getDescription());
				
				break;
			}
			
			
			
		}
		//when clicked on the link
		if(v.getId()==R.id.click){
			
			inte.setData(Uri.parse(newItems.get(k).getLink()));
			
			startActivity(inte);	
		}
		
		//when clicked on the logo.
		if(v.getId()==R.id.widget41){
		
		this.finish();
		
		Intent bToHome= new Intent(this,MainQNews.class);
		
		startActivity(bToHome);
		
		finish();
		
		}
	}
}